package Servelt;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.SimpleFormatter;

public class Servlet extends javax.servlet.http.HttpServlet {
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        String lat=null;
        Cookie[] cookies=request.getCookies();
        System.out.println(cookies);
        for(int i=0;cookies!=null&&i<cookies.length;i++)
        {
            if("la".equals(cookies[i].getName()))
            {
                lat=cookies[i].getValue();
                break;
            }
        }
        System.out.println(lat);
        if (lat==null)
        {
            response.getWriter().println("你是首次访问！");
        }else
        {
            response.getWriter().println("您上次访问时间是："+lat);
        }
        String time=new SimpleDateFormat("yyyy-MM-dd-hh:mm:ss").format(new Date());
        Cookie cookie=new Cookie("la",time);
        cookie.setMaxAge(-1);
        response.addCookie(cookie);
    }
}
